//
//  ShippedTaskCell.m
//  RRQS
//
//  Created by kyjun on 16/3/25.
//
//

#import "ShippedTaskCell.h"

@interface ShippedTaskCell ()
@property(nonatomic,strong) UIButton* btnIcon;
@property(nonatomic,strong) UILabel* labelContent;
@property(nonatomic,strong) UIButton* btnStatus;
@property(nonatomic,strong) UIImageView* line;
@end

@implementation ShippedTaskCell

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.backgroundColor = [UIColor colorWithRed:255/255.f green:255/255.f blue:255/255.f alpha:1.0];
        [self layoutUI];
        [self layoutConstraints];
    }
    return self;
}

#pragma mark =====================================================  user interface layout
-(void)layoutUI{
    [self addSubview:self.btnIcon];
    [self addSubview:self.btnStatus];
    [self addSubview:self.labelContent];
    [self addSubview:self.line];
}

-(void)layoutConstraints{
    self.btnIcon.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnStatus.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelContent.translatesAutoresizingMaskIntoConstraints = NO;
    self.line.translatesAutoresizingMaskIntoConstraints = NO;
    
    CGFloat iconHeight = CGRectGetHeight(self.frame)/3;
    
    [self.btnIcon addConstraint:[NSLayoutConstraint constraintWithItem:self.btnIcon attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:iconHeight]];
    [self.btnIcon addConstraint:[NSLayoutConstraint constraintWithItem:self.btnIcon attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:iconHeight]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.btnIcon attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.f]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.btnIcon attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10.f]];
    
    [self.btnStatus addConstraint:[NSLayoutConstraint constraintWithItem:self.btnStatus attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH/4]];
    [self.btnStatus addConstraint:[NSLayoutConstraint constraintWithItem:self.btnStatus attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:30.f]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.btnStatus attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.f]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.btnStatus attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10.f]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.labelContent attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.f]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.labelContent attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.btnIcon attribute:NSLayoutAttributeRight multiplier:1.0 constant:10.f]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.labelContent attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.labelContent attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.btnStatus attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
    
    [self.line addConstraint:[NSLayoutConstraint constraintWithItem:self.line attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:1.0f]];
    [self.line addConstraint:[NSLayoutConstraint constraintWithItem:self.line attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.line attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.line attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];

}

#pragma mark =====================================================  SEL
-(void)btnStatusTouch:(UIButton *)sender{
    if ([self.type isEqualToString:@"1"]) {
        if(self.delegate && [self.delegate respondsToSelector:@selector(shippedTaskListItemConfirm:)]){
            [self.delegate shippedTaskListItemConfirm:self.waiMaiDodelListitems];
        }
    }else{
        if(self.delegate && [self.delegate respondsToSelector:@selector(shippedTaskListConfirm:)]){
            [self.delegate shippedTaskListConfirm:self.waiMaiDodelHList];
        }
    }
   
}

#pragma mark =====================================================  property package
- (void)setWaiMaiDodelHList:(WJWaiMaiDodelHList *)waiMaiDodelHList
{
    if(waiMaiDodelHList){
        _waiMaiDodelHList = waiMaiDodelHList;
        NSString*strMoney = [NSString stringWithFormat:@"%@ \n%@",waiMaiDodelHList.title,waiMaiDodelHList.map_location];
        NSMutableAttributedString* attributeStr  = [[NSMutableAttributedString alloc]initWithString:strMoney];
        
        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.f],NSForegroundColorAttributeName:[UIColor colorWithRed:0/255.f green:0/255.f blue:0/55.f alpha:1.0]} range:[strMoney rangeOfString:waiMaiDodelHList.title]];
        self.labelContent.attributedText = attributeStr;
        
        self.btnStatus.userInteractionEnabled = YES;
        [self.btnStatus setBackgroundImage:[UIImage imageNamed:@"icon-btn-bg-enter"] forState:UIControlStateNormal];
        [self.btnStatus setTitle:@"确认送达" forState:UIControlStateNormal];
        [self.btnIcon setImage:[UIImage imageNamed:@"car@2x"] forState:UIControlStateNormal];
    }
}
- (void)setWaiMaiDodelListitems:(WJWaiMaiDodelListitems *)waiMaiDodelListitems
{

    if(waiMaiDodelListitems){
        _waiMaiDodelListitems = waiMaiDodelListitems;
        NSString*strMoney = [NSString stringWithFormat:@"%@ \n%@",waiMaiDodelListitems.site_name,waiMaiDodelListitems.site_addr];
        NSMutableAttributedString* attributeStr  = [[NSMutableAttributedString alloc]initWithString:strMoney];
        
        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.f],NSForegroundColorAttributeName:[UIColor colorWithRed:0/255.f green:0/255.f blue:0/55.f alpha:1.0]} range:[strMoney rangeOfString:waiMaiDodelListitems.site_name]];
        self.labelContent.attributedText = attributeStr;
        
        self.btnStatus.userInteractionEnabled = YES;
        [self.btnStatus setBackgroundImage:[UIImage imageNamed:@"icon-btn-bg-enter"] forState:UIControlStateNormal];
        [self.btnStatus setTitle:@"确认送达" forState:UIControlStateNormal];
        [self.btnIcon setImage:[UIImage imageNamed:@"icon-sendStatus-enter"] forState:UIControlStateNormal];
    }
}

-(void)setEntity:(MSubOrder *)entity{
    if(entity){
        _entity = entity;
        NSString*strMoney = [NSString stringWithFormat:@"%@ \n%@",entity.storeName,entity.storeAddress];
        NSMutableAttributedString* attributeStr  = [[NSMutableAttributedString alloc]initWithString:strMoney];
     //   [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.f],NSForegroundColorAttributeName:[UIColor colorWithRed:193/255.f green:193/255.f blue:193/55.f alpha:1.0]} range:[strMoney rangeOfString:entity.storeAddress]];
        
         [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.f],NSForegroundColorAttributeName:[UIColor colorWithRed:0/255.f green:0/255.f blue:0/55.f alpha:1.0]} range:[strMoney rangeOfString:entity.storeName]];
        self.labelContent.attributedText = attributeStr;
        if([entity.sendStatus isEqualToString:@"1"]){
            self.btnIcon.selected = YES;
        }else {
            if (entity.status == expressStatusIng ){
                self.btnStatus.userInteractionEnabled = YES;
                 [self.btnStatus setBackgroundImage:[UIImage imageNamed:@"icon-btn-bg-enter"] forState:UIControlStateNormal];
                [self.btnStatus setTitle:@"确认送达" forState:UIControlStateNormal];
                 [self.btnIcon setImage:[UIImage imageNamed:@"icon-sendStatus-enter"] forState:UIControlStateNormal];
            }else if(entity.status == expressStatusWating ){
               
                self.btnStatus.selected = NO;
            }else if (entity.status == expressStatusFinished ){
                self.btnStatus.userInteractionEnabled = NO;
                [self.btnStatus setBackgroundImage:[UIImage imageNamed:@"icon-btn-bg-default"] forState:UIControlStateNormal];
                [self.btnStatus setTitle:@"已完成" forState:UIControlStateNormal];
                self.labelContent.attributedText =[[NSMutableAttributedString alloc]initWithString:strMoney];
                [self.btnIcon setImage:[UIImage imageNamed:@"icon-sendStatus-enter"] forState:UIControlStateNormal];
            }else if (entity.status == expressStatusCancelled|entity.status == expressStatusCanceling ){
                self.btnStatus.userInteractionEnabled = NO;
                [self.btnStatus setBackgroundImage:[UIImage imageNamed:@"icon-btn-bg-default"] forState:UIControlStateNormal];
                [self.btnStatus setTitle:@"已取消" forState:UIControlStateNormal];
                self.labelContent.attributedText =[[NSMutableAttributedString alloc]initWithString:strMoney];
                  [self.btnIcon setImage:[UIImage imageNamed:@"icon-sendStatus-default"] forState:UIControlStateNormal];
            }
        }
        
    }
}

-(UIButton *)btnIcon{
    if(!_btnIcon){
        _btnIcon = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnIcon setImage:[UIImage imageNamed:@"icon-sendStatus-enter"] forState:UIControlStateNormal];
    
    }
    return _btnIcon;
}

-(UILabel *)labelContent{
    if(!_labelContent){
        _labelContent = [[UILabel alloc]init];
        _labelContent.font = [UIFont systemFontOfSize:14.f];
        _labelContent.numberOfLines = 0;
        _labelContent.lineBreakMode = NSLineBreakByCharWrapping;
        _labelContent.textColor =  [UIColor colorWithRed:113/255.f green:113/255.f blue:113/255.f alpha:1.0];
    }
    return _labelContent;
}

-(UIButton *)btnStatus{
    if(!_btnStatus){
        _btnStatus = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnStatus setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btnStatus setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];

        [_btnStatus setBackgroundImage:[UIImage imageNamed:@"icon-btn-bg-enter"] forState:UIControlStateNormal];
        [_btnStatus setTitle:@"确认送达" forState:UIControlStateNormal];
     
        _btnStatus.titleLabel.font = [UIFont systemFontOfSize:14.f];
        [_btnStatus addTarget:self action:@selector(btnStatusTouch:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnStatus;
}

-(UIImageView *)line{
    if(!_line){
        _line = [[UIImageView alloc]init];
        _line.backgroundColor =  [UIColor colorWithRed:231/255.0f green:231/255.0f blue:231/255.0f alpha:1.0f];
    }
    return _line;
}

@end
